import React from "react";
import { consts } from "@/module-00-codebase/pkg-00-const";
import { BaseElmt } from "@/module-00-codebase/pkg-03-abstract";
import { Bi, Ei } from "@/module-00-codebase/pkg-01-container";
import * as style from "./style";
export class Elmt extends BaseElmt<[{ src?: string; location?: string } | string], Elmt> {
  private src: string = "/icon/no-image.png";
  private style: { [key: string]: any } = style.noImgStyle;
  private params_0_cache: any;
  public elmtView(): JSX.Element {
    const fileObj = this.getParams(0);
    let location: string | undefined;
    if (typeof fileObj === "string" && fileObj === this.params_0_cache) {
      //skip
    } else if (typeof fileObj !== "string" && fileObj.location === this.params_0_cache.location) {
      //skip
    } else {
      this.params_0_cache = fileObj;
      if (typeof fileObj === "string") {
        this.style = style.imgStyle;
        this.src = fileObj;
      } else if (!(location = fileObj.location)) {
        this.style = style.noImgStyle;
        this.src = "/icon/no-image.png";
      } else {
        (async () => {
          const src_res = await Bi.rpc.send({ resourceName: "innerFileContent", action: "getone", data: { id: location }, loading: false });
          this.style = style.imgStyle;
          this.src = this.downloadSrc(src_res?.data?.data?.src, location);
          this.elmtRefresh();
        })();
      }
    }
    return <Ei.Layout params={[]} className={this.style} style={{ backgroundImage: `url(${this.src})` }} />;
  }
  private downloadSrc(src: string, location: string): string {
    return src || `${consts.HTTP_BASE_PATH}/resource/innerFileContent/getone/${location}?download`;
  }
}
